package bog

class User {
    /*************************************/
    /** relationships                   **/
    /*************************************/
    static hasMany = [
            questions: Question,
            answers: Answer,
            threads: Thread,
    ]
    /*************************************/

    String firstName;
    String lastName;
    Date birthDate;
    String email;
    Department department;
    String type = "User";

    String username;
    String password;
    Date lastLogin;

    static constraints = {
        firstName(blank: false)
        lastName(blank: false)
        birthDate(
                min: Date.parse('dd-MM-yyyy', '01-01-1900'),
                max: Date.parse('dd-MM-yyyy', '01-01-1995')
        )
        email(email: true, blank: false)
        department(nullable: false)
        username(blank: false, size: 5..14, unique: true)
        password(blank: false, length: 6..12,
                 validator: { val, obj ->
                    if (val?.equalsIgnoreCase(obj.username)) {
                        return "user error"
                    }
                 })
        lastLogin(nullable: true)
    }

    int getReputation() {
        int reputation = 0;

        answers.each {reputation += it.points}

        log.info("reputation: " + reputation)

        return reputation
    }
}
